//
//  RootViewController.m
//  E_ChatRoom
//
//  Created by rimi1 on 15/6/25.
//  Copyright (c) 2015年 TJ. All rights reserved.
//

#import "RootViewController.h"
#import "DiscoverViewController.h"
#import "ChatRoomViewController.h"
#import "FriendsViewController.h"
#import "MessageViewController.h"
#import "MineViewController.h"

@interface RootViewController ()

@property (nonatomic, strong)NSArray *normImgNameArray;
@property (nonatomic, strong)NSArray *seletedImgNameArray;
@end

@implementation RootViewController

- (NSArray *)normImgNameArray {
    
    if (_normImgNameArray == nil) {
        
        _normImgNameArray = @[@"tabbar_findyuwan_button",@"tabbar_message_button",@"tabbar_pagehome_button_gray",@"tabbar_circle_button",@"tabbar_mine_button"];
    }
    return _normImgNameArray;
}

- (NSArray *)seletedImgNameArray{
    
    if (_seletedImgNameArray == nil) {
        
        _seletedImgNameArray =  @[@"tabbar_findyuwan_button_selected",@"tabbar_message_button_selected",@"tabbar_pagehome_button",@"tabbar_circle_button_selected",@"tabbar_mine_button_selected"];}
    return _seletedImgNameArray;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.view.backgroundColor = [UIColor whiteColor];
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 1.初始化子控制器
    DiscoverViewController *dicover = [[DiscoverViewController alloc] init];
    [self addChildVc:dicover title:@"发现" image:self.normImgNameArray[0] selectedImage:self.seletedImgNameArray[0]];
    
    MessageViewController *message = [[MessageViewController alloc] init];
    [self addChildVc:message title:@"消息" image:self.normImgNameArray[1]selectedImage:self.seletedImgNameArray[1]];
    
    ChatRoomViewController *chatRoom = [[ChatRoomViewController alloc] init];
    [self addChildVc:chatRoom title:@"聊天室" image:self.normImgNameArray[2] selectedImage:self.seletedImgNameArray[2]];
    
    FriendsViewController *friends = [[FriendsViewController alloc] init];
    [self addChildVc:friends title:@"好友" image:self.normImgNameArray[3] selectedImage:self.seletedImgNameArray[3]];
    
    MineViewController *mine = [[MineViewController alloc] init];
    [self addChildVc:mine title:@"我" image:self.normImgNameArray[4] selectedImage:self.seletedImgNameArray[4]];
    
}

/**
 *  添加一个子控制器
 *
 *  @param childVc       子控制器
 *  @param title         标题
 *  @param image         图片
 *  @param selectedImage 选中的图片
 */
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置子控制器的文字
    childVc.title = title;
    
    // 同时设置tabbar和navigationBar的文字
    //childVc.tabBarItem.title = title; // 设置tabbar的文字
    //childVc.navigationItem.title = title; //设置navigationBar的文字
    
    // 设置子控制器的图片
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 设置文字的样式
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = RGBColor(123, 123, 123);
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    
    childVc.view.backgroundColor = RandomColor;
    
    // 给外面传进来的视图控制器包装一个导航控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:childVc];
    // 添加为子控制器
    [self addChildViewController:nav];
}

@end
